// app.js
App({
	onLaunch() {
		let statusBarHeight = wx.getSystemInfoSync().statusBarHeight
		let info = wx.getMenuButtonBoundingClientRect()
		this.globalData.navInfo = {
			statusHeight: statusBarHeight,
			top: info.top,
			height: info.height,
			left: info.left //胶囊左边的width
		}

		// 版本更新
		wx.getUpdateManager().onCheckForUpdate(function(res) {
			// 请求完新版本信息的回调
			if (res.hasUpdate) {
				// 小程序有新版本，会主动触发下载操作（无需开发者触发）
				wx.getUpdateManager().onUpdateReady(function() { //当新版本下载完成，会进行回调
					// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
					wx.getUpdateManager().applyUpdate();
				})
				// 小程序有新版本，会主动触发下载操作（无需开发者触发）
				wx.getUpdateManager().onUpdateFailed(function() { //当新版本下载失败，会进行回调
					wx.showModal({
						title: '提示',
						content: '检查到有新版本，但下载失败，请检查网络设置',
						showCancel: false,
					})
				})
			}
		});

	},
	//链接全局socket
	connectSocket() {
		var that = this
		if (!this.globalData.userInfo.uid) {
			// return wx.showT 
			return ;
		}
		// console.log("连接次数a", this.globalData.number)
		if (this.globalData.number >= 3) {
			// console.log("连接了3次", this.globalData.number)
			return;
		}
		this.globalData.SocketTask = wx.connectSocket({
			// url: 'wss://www.yunanhua.com//wss?client=user&id=' + this.globalData.userInfo.uid,
			url: 'wss://www.yunanhua.com//wss?client=user&id=u_' + this.globalData.userInfo.uid,
			header: {
				'content-type': 'application/json'
			},
			method: 'post',
			success: function(res) {

				// console.log('WebSocket连接创建', res)
			},
			fail: function(err) {
				// console.log("连接失败", err)
				// this.webSocket()
			},
		})
		this.globalData.SocketTask.onOpen(res => {
			that.globalData.number = 0 //连接成功重置连接次数
			that.globalData.showSocket = true
			if (that.globalData.againSocket == 1) {
				wx.showToast({
					title: '已连接',
					icon: 'none',
					duration: 1000
				})
			}

			// console.log('监听 WebSocket 连接打开事件。', res)
		})
		this.globalData.SocketTask.onClose(err => {
			// console.log("监听socket关闭信息", err)
			that.globalData.showSocket = false
			that.connectSocket() //重新连接
		})
		//监听socket错误信息
		this.globalData.SocketTask.onError(err => {
			// console.log("监听socket错误信息", err)
			that.globalData.showSocket = false
			if (that.globalData.againSocket == 1) {
				wx.showToast({
					title: '连接失败',
					icon: 'none',
					duration: 1000
				})
			}
			// that.connectSocket()//重新连接
		})

		this.globalData.number++
		// console.log("连接次数", this.globalData.number)



		this.globalData.SocketTask.onMessage(onMessage => {
			// console.log('监听WebSocket接受到服务器的消息事件。服务器返回的消息index', (JSON.parse(onMessage.data)))
			var msg = JSON.parse(onMessage.data)
			if (msg.type == 'ping') {
				var heartbeat = {
					type: 'pong',
					content: '心跳检测'
				}
				getApp().globalData.SocketTask.send({
					data: JSON.stringify(heartbeat),
					success: function(res) {
						// console.log('发送成功', res)
					},
					fail: function(err) {
						// console.log("发送失败", err)
						that.connectSocket()

					},
				})
			}
			// else if(msg.type=="total_msg"){
			// this.globalData.total_msg=msg.total_count;
			// // var totalMsg=msg.total_count
			// // that.setData({
			// // 	totalMsg:totalMsg
			// // })
			//  }
			else {
				if (this.globalData.onMessage) {
					this.globalData.onMessage(msg);
				}
			}
			if (msg.type == "total_msg") {
				this.globalData.msgCount = msg.total_count
			}
		})



    },
    onShow: function (options) {
        // if (options.referrerInfo && options.referrerInfo.extraData) {
        //   this.globalData.backMerchantUrl = options.referrerInfo.extraData.backMerchantUrl;
        //   wx.reLaunch({
        //     url: '/pages/tabbar/my/my'
        //   });
        // }
      },
	globalData: {
        pattern: /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
		navInfo:null,
		number: 0, //socket连接次数
		msgCount: 0, //未读总数
		onMessage: "",
		SocketTask: "", //socket连接
		showSocket: false,
		againSocket: 0, //1重连
		friendList: [],
		myInfo: {},
		/**用户的商家信息 */
		supplier: {},
		userInfo: null,
        cateArr: null, //分类列表
        cateArr2:null,//拼单分类
		/**当前信息仓库 */
		warehouse: {
			wh_id: 1,
			title: "昆明总仓"
		},
		/**仓库列表 */
		areaData: [],

		/**等级列表 */
		levelArr: [
			{
				name: 'AA',
				text: 'AA',
				value: 'AA',
			},
			{
				name: 'A',
				text: 'A',
				value: 'A',
			},
			{
				name: 'B',
				text: 'B',
				value: 'B'
			},
			{
				name: 'C',
				text: 'C',
				value: 'C',
			},
			{
				name: '基地',
				text: '基地',
				value: '基地',
			},
			{
				name: 'D',
				text: 'D',
				value: 'D',
			},
		],
		/**成熟度 */
		maturityArr: [{
				text: '生',
				value: '生',
				name: '生',
			},
			{
				text: '中熟',
				value: '中熟',
				name: '中熟',
			},
			{
				text: '偏熟',
				value: '偏熟',
				name: '偏熟',
			}
		],
		/**入驻商家拒绝原因 */
		refuse: '',
		config1: {
			/**客服电话 */
			mobile: "",
			name: "",
		},
		config2: {
			/**每日花价 */
			flower_price: "",
			/**开始时间 */
			trade_ltime: "",
			/**结束时间 */
			trade_stime: ""
		},
		/**员工用款类型 */
		billTypeArr: []
	}
})
